/**
 * <strong>Title : RepairCategoryController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月5日 下午1:52:45<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.condition.RepairCategoryCondition;
import cn.b.sky.mes.em.service.RepairCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.RepairCategory;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : RepairCategoryController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>维修类别控制器<br> 
 * <strong>Create on : 2015年11月5日 下午1:52:45<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/repairCategory/*")
public class RepairCategoryController extends SecurityController {

  @Resource
  RepairCategoryService repairCategoryService;

  @RequestMapping("repairCategory_list")
  public String selectRepairCategoryListByCondition(@ModelAttribute("condition") RepairCategoryCondition condition,
      String navTabId, Model model) {
    condition.setSiteCode(getSiteCode());
    List<RepairCategory> list = repairCategoryService.selectRepairCategoryListByCondition(condition);
    int count = repairCategoryService.selectRepairCategoryCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "em/repairCategory/repairCategory_list";
  }

  @RequestMapping("addRepairCategory")
  public String addRepairCategory(String navTabId, Model model) {

    model.addAttribute("pattern", "add");
    model.addAttribute(NAVTABID, navTabId);
    return "em/repairCategory/repairCategory_add";
  }

  @RequestMapping("modifyRepairCategory")
  public String addRepairCategory(String navTabId, Model model, String id) {
    RepairCategory repairCategory = repairCategoryService.getRepairCategory(id, getSiteCode());
    model.addAttribute("pattern", "modify");
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("repairCategory", repairCategory);
    return "em/repairCategory/repairCategory_add";
  }

  @RequestMapping("validateRepairCategoryCode")
  @ResponseBody
  public boolean validateRepairCategoryCode(String categoryCode) {
    if (!StringUtils.isEmpty(categoryCode)) {
      RepairCategory repairCategory = repairCategoryService.getRepairCategory(categoryCode, getSiteCode());

      return repairCategory == null;
    } else {
      return true;
    }
  }

  @RequestMapping("saveRepairCategory")
  @ResponseBody
  public DWZResult saveRepairCategory(RepairCategory repairCategory, String navTabId, String id) {
    if (repairCategory.getCategoryCode() == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    repairCategory.setSiteCode(getSiteCode());
    setCreateInfo(repairCategory);
    if (repairCategory.getLockFlag() == null) {
      repairCategory.setLockFlag(0);
    }
    if (StringUtils.isBlank(id)) {
      repairCategoryService.insertRepairCategory(repairCategory);
    } else {
      repairCategory.setCategoryCode(id);
      repairCategoryService.updateRepairCategory(repairCategory);
    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);
  }

  @RequestMapping("viewRepairCategory")
  public String viewRepairCategory(String navTabId, Model model, String id) {
    RepairCategory repairCategory = repairCategoryService.getRepairCategory(id, getSiteCode());
    model.addAttribute("pattern", "read");
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("repairCategory", repairCategory);
    return "em/repairCategory/repairCategory_add";
  }

  @RequestMapping("deleteRepairCategory")
  @ResponseBody
  public DWZResult deleteRepairCategory(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] codes = ids.split(",");
    int len = codes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(len);
    boolean success = true;
    for (int i = 0; i < len; i++) {
      BatchJobReport report = repairCategoryService.deleteRepairCategory(codes[i], getSiteCode());
      reports.add(report);
      if (!report.isSuccess()) {
        success = true;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  @RequestMapping(value = "repairCategoryLookup")
  public String repairCategoryLookup(String navTabId, Model model,
      @ModelAttribute("condition") RepairCategoryCondition condition) {
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    condition.setLockFlag(0);
    List<RepairCategory> categoryList = repairCategoryService.selectRepairCategoryListByCondition(condition);
    int count = repairCategoryService.selectRepairCategoryCount(condition);
    condition.setData(categoryList);
    condition.setTotalCount(count);
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    model.addAttribute("page", condition);
    return "em/repairCategory/repairCategory_lookup";
  }

  @RequestMapping(value = "repairCategoryDropdown")
  @ResponseBody
  public Object repairCategoryDropdown(String navTabId, Model model,
      @ModelAttribute("condition") RepairCategoryCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    return repairCategoryService.selectRepairCategoryForSuggest(condition);
  }

  @RequestMapping("checkRepairCategoryCode")
  @ResponseBody
  public boolean checkRepairCategoryCode(String repairCategoryCode) {
    if (!StringUtils.isEmpty(repairCategoryCode)) {
      RepairCategory repairCategory = repairCategoryService.getRepairCategory(repairCategoryCode, getSiteCode());
      return repairCategory != null && repairCategory.getLockFlag() != 1;
    } else {
      return true;
    }
  }

}
